<?php

namespace App\Controller;

use App\Entity\Category;
use App\Entity\Product;
use App\Service\MessageGenerator;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
 * Class TestController
 * @package App\Controller
 * @Route("/test")
 */
class TestController extends AbstractController
{
    private $session;

    public function __construct(SessionInterface $session, LoggerInterface $logger)
    {
        $logger->info('hhhhh');
        $this->session = $session;
    }

    /**
     * @Route("/", name="test")
     */
    public function index()
    {
        return $this->render('test/index.html.twig', [
            'controller_name' => 'TestController',
        ]);
    }

    /**
     * @Route("/test")
     */
    public function test()
    {

        $category = new Category();
        $category->setName('水果');

        $product = new Product();
        $product->setName('苹果')
            ->setPrice(12.25)
            ->setCategory($category);
        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->persist($category);
        dump($product);
        $em->flush();


//        dump($product->getId());
//        dump($category->getId());

        /*$em = $this->getDoctrine()->getManager();
        $model = $em->getRepository(Product::class);
        $product = $model->find(1);
        if (!$product){
            throw $this->createNotFoundException('No product');
        }
        $product->setName('哈哈哈哈哈哈但是风格')->setPrice(100);
//        $em->remove($product);
        $em->flush();
        */
        return $this->render('test/test.html.twig', ['body'=>sprintf("%s, %s", $product->getId(), $category->getId())]);
    }

    /**
     * @Route("/product/{id}")
     * @param $id
     * @return Response
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function showProduct($id) : Response
    {
//        $product = $this->getDoctrine()->getRepository(Product::class)->find($id);
//        if (!$product) throw $this->createNotFoundException('商品不存在');

//        dump($product);

        $product = $this->getDoctrine()->getRepository(Product::class)
            ->findOneByIdJoinedToCategory($id);

        dump($product);

        return $this->render('base.html.twig');
    }

    /**
     * @Route("/category/{id}/products")
     * @param $id
     * @return Response
     */
    public function showProducts($id) : Response
    {
        $category = $this->getDoctrine()->getRepository(Category::class)->find($id);
        if (!$category) throw $this->createNotFoundException('没有分类');

        $products = $category->getProducts();

        dd($products);
    }

    /**
     * @Route("/params")
     * @param Request $request
     * @return Response
     */
    public function params(Request $request)
    {
        $name = $request->query->get('name');
        return new Response("Hello ${name}");
    }

    /**
     * @Route("/404")
     */
    public function err_404()
    {
        throw $this->createNotFoundException('没了');
    }

    /**
     * @Route("/session_set")
     */
    public function session_set()
    {
        $this->session->set("name", "的风格都是规范");
        return $this->json(["data"=>"success"]);
    }

    /**
     * @Route("/session/{name}")
     * @param SessionInterface $session
     * @param $name
//     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @return Response
     */
    public function session_get(SessionInterface $session, $name)
    {
        $name = $session->get($name);
        dump($_ENV);
        dump($name);
        return new Response();
    }
}
